package com.wanxing.main;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zhny.wxginger.R;
import com.zhny.handler.Sendcommand;

import sim.Sim;

@SuppressLint("NewApi")
public class ControlFragmentNew extends Fragment implements OnClickListener {
    private String plotid = null;
    private Handler handler;
    private ArrayAdapter<String> lab_adapter;
    private boolean flag = false;
    private Boolean s_switch = false;

    // Button b2;// 保存
    // Button b3;// 保存
    private Button c_s_open;
    private Button c_s_close;
    private Button c_f_open;
    private Button c_f_close;
    private Button c_z_open;
    private Button c_z_close;
    private Button c_all_open;
    private Button c_all_close;
    private Button c_feng_open;
    private Button c_feng_close;
    private String labname;

    private static SharedPreferences preferences;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater
                .inflate(R.layout.control_set_new, container, false);
        viewFind(view);
        // viewSet();
        labname = getActivity().getActionBar().getTitle().toString();
        plotid = labname;
        return view;
    }

    public void viewFind(View view) {
        c_s_open = (Button) view.findViewById(R.id.c_s_open);
        c_s_close = (Button) view.findViewById(R.id.c_s_close);
        c_f_open = (Button) view.findViewById(R.id.c_f_open);
        c_f_close = (Button) view.findViewById(R.id.c_f_close);
        c_z_open = (Button) view.findViewById(R.id.c_z_open);
        c_z_close = (Button) view.findViewById(R.id.c_z_close);
        c_all_open = (Button) view.findViewById(R.id.c_all_open);
        c_feng_open = (Button) view.findViewById(R.id.c_fengshan_open);
        c_feng_close = (Button) view.findViewById(R.id.c_fengshan_close);

        c_s_open.setOnClickListener(this);
        c_s_close.setOnClickListener(this);
        c_f_open.setOnClickListener(this);
        c_f_close.setOnClickListener(this);
        c_z_open.setOnClickListener(this);
        c_z_close.setOnClickListener(this);
        c_all_open.setOnClickListener(this);

        c_feng_open.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String message = getActivity().getResources().getText(R.string.openmessage).toString().trim();
                Sim sim = new Sim(message);
                sim.sendMessage();
                Toast.makeText(getActivity(), "已成功发送短信--打开", Toast.LENGTH_SHORT).show();
            }
        });
        c_feng_close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String message = getActivity().getResources().getText(R.string.closemessage).toString().trim();
                Sim sim = new Sim(message);
                sim.sendMessage();
                Toast.makeText(getActivity(), "已成功发送短信--关闭", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void showDialog2(final int split)
    {
        LayoutInflater inflater = LayoutInflater.from(this.getActivity());
        final View view = inflater.inflate(R.layout.dialog_all_water, null);
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this.getActivity());
        builder.setTitle(plotid + " -- 请输入时间");
        builder.setView(view);

        preferences = getActivity().getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);


        final EditText t_before = (EditText) view.findViewById(R.id.d_all_before_time_e);
        final EditText t_ing = (EditText) view.findViewById(R.id.d_all_time_e);
        final EditText t_latter = (EditText) view.findViewById(R.id.d_all_latter_time_e);

        //初始化参数
        t_before.setText(preferences.getString(plotid + "time_before", "0"));
        t_ing.setText(preferences.getString(plotid + "time_ing", "0"));
        t_latter.setText(preferences.getString(plotid + "time_latter", "0"));

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            EditText id = (EditText) view.findViewById(R.id.d_all_yanzhengma);


            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                String command;
                String sid = id.getText().toString().trim();
                System.out.println(sid);
                String s_t_be = t_before.getText().toString().trim();
                String s_t_ing = t_ing.getText().toString().trim();
                String s_t_la = t_latter.getText().toString().trim();

                String all = "" + (Integer.parseInt(s_t_be) + Integer.parseInt(s_t_ing) + Integer.parseInt(s_t_la));

                System.out.println(all + "@##@#@#");

                if (sid.equals("112233")) {
                    if (plotid.equals("006")) {

                        //测试
                        System.out.println("i am here");
                        //指令
                        command = "BB01dezhoulx" + "006" + "#15" + s_t_be + ":" + s_t_ing + ":" + all + ":$";// 手动浇水施肥打开  - 6号棚
                        System.out.println(command);
                        //发送指令
                        new Thread(new Sendcommand(handler, command))
                                .start();
                    } else {

                        command = "BB01dezhoulx" + "006" + "#15" + 0 + ":" + 0 + ":" + all + ":$";// 手动浇水施肥打开
                        System.out.println(split + "  " + command);
                        new Thread(new Sendcommand(handler, command))
                                .start();

                        command = "BB01dezhoulx" + plotid + "#15" + s_t_be + ":" + s_t_ing + ":" + all + ":$";// 手动浇水施肥打开
                        System.out.println(split + "  " + command);
//                        new Thread(new Sendcommand(handler, command))
//                                .start();
                    }
                    //保存各大棚参数
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString(plotid + "time_before", s_t_be);
                    editor.putString(plotid + "time_ing", s_t_ing);
                    editor.putString(plotid + "time_latter", s_t_la);
                    editor.commit();

                } else {
                    Toast.makeText(getActivity(), "验证失败", Toast.LENGTH_SHORT).show();
                }
            }

        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();


    }

    private void showDialog1(final int split) {
        LayoutInflater inflater = LayoutInflater.from(this.getActivity());
        final View view = inflater.inflate(R.layout.dialog_item, null);
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this.getActivity());
        builder.setTitle("请输入密码进行验证");
        builder.setView(view);
        builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                EditText id = (EditText) view.findViewById(R.id.ed_id);
                String sid = id.getText().toString();
                if (sid.equals("112233")) {
                    // 发送指令
                    String command;
                    switch (split) {
                        case 1:
                            command = "BB00dezhoulx" + "006" + "AE";// 水泵电机打开
                            System.out.println(split + "  " + command);
//                            new Thread(new Sendcommand(handler, command)).start();
                            break;
                        case 2:
                            command = "BB00dezhoulx" + "006" + "AF";// 关闭
                            System.out.println(split + "  " + command);
//                            new Thread(new Sendcommand(handler, command)).start();
                            break;
                        case 3:
                            command = "BB00dezhoulx" + plotid + "AC";// 肥料电机打开
                            System.out.println(split + "  " + command);
//                            new Thread(new Sendcommand(handler, command)).start();
                            break;
                        case 4:
                            command = "BB00dezhoulx" + plotid + "AD";// 关闭
                            System.out.println(split + "  " + command);
//                            new Thread(new Sendcommand(handler, command)).start();
                            break;
                        case 5:
                            command = "BB00dezhoulx" + plotid + "AA";// 主水管电池阀打开
                            System.out.println(split + "  " + command);
//                            new Thread(new Sendcommand(handler, command)).start();
                            break;
                        case 6:
                            command = "BB00dezhoulx" + plotid + "AB";// 关闭
                            System.out.println(split + "  " + command);
//                            new Thread(new Sendcommand(handler, command)).start();
                            break;
                    }
                } else {
                    Toast.makeText(getActivity(), "验证失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
            }
        });
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

    }

    public void viewSet() {

		/*
         * b2.setOnClickListener(new View.OnClickListener() {
		 *
		 * @Override public void onClick(View v) { // flag = false; Intent
		 * intent = new Intent(getActivity(), Dialog_water_Activity.class);
		 *
		 * intent.putExtra("dtu", plotid); startActivity(intent);
		 *
		 * } });
		 *
		 * b3.setOnClickListener(new OnClickListener() {
		 *
		 * @Override public void onClick(View arg0) { // TODO Auto-generated
		 * method stub //showDialog3(); Intent intent = new
		 * Intent(getActivity(), Dialog_time_Activity.class);
		 * startActivity(intent); } });
		 */
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String command;
        int p = 0;
        switch (v.getId()) {
            case R.id.c_s_open:
                p = 1;
                showDialog1(p);
                break;
            case R.id.c_s_close:
                p = 2;
                showDialog1(p);
                break;
            case R.id.c_f_open:
                p = 3;
                showDialog1(p);
                break;
            case R.id.c_f_close:
                p = 4;
                showDialog1(p);
                break;
            case R.id.c_z_open:
                p = 5;
                showDialog1(p);
                break;
            case R.id.c_z_close:
                p = 6;
                showDialog1(p);
                break;
            case R.id.c_all_open:
                p = 7;
                showDialog2(p);
                break;
        }

    }
    /*
     * class myAsyncTask2 extends AsyncTask<String, Void, String> {
	 *
	 * @Override protected String doInBackground(String... params) { // TODO
	 * Auto-generated method stub String result =
	 * HttpUtils.sendPostMethod(params[0], "utf-8");
	 * System.out.println(result+"--------get 水泵电机"); return result; }
	 *
	 * @Override protected void onPostExecute(String result) { // TODO
	 * Auto-generated method stub super.onPostExecute(result);
	 *
	 * if (result == null) { myswitch.setChecked(false); s_switch = false; }
	 * else if (result.equals("10.00")) { myswitch.setChecked(true); s_switch =
	 * true; } else if (result.equals("00.00")) { myswitch.setChecked(false);
	 * s_switch = false; } }
	 *
	 * }
	 */

}
